package com.mini.demos.webgame.db;

import java.util.Date;
import java.util.List;

import com.mini.demos.webgame.entity.Job;
import com.mini.demos.webgame.entity.User;
import com.mini.demos.webgame.msg.JobMsg;
import com.mini.demos.webgame.util.JobStatEnum;

public class JobService {
	
	/**
	 * 保存Job.
	 * 
	 * @param msg
	 */
	public void saveJob(JobMsg msg){
		Job job = new Job();
		Date now = new Date();
		job.setJobId(msg.getJobId().longValue());
		job.setUserName(msg.getUserName());
		job.setJobItemPath(msg.getJobItemPath());
		job.setCostPrice(msg.getCostPrice().floatValue());
		job.setStartDate(now);
		job.setModifyDate(now);
		job.setCostPrice(msg.getCostPrice().floatValue());
		job.setGetPrice(msg.getGetPrice().floatValue());
		job.setState(JobStatEnum.START.getStatCode());//状态
		String sql = "insert into t_job(jobId,userName,jobItemPath,startDate,endDate,modifyDate,costPrice,getPrice,state) values(?,?,?,?,?,?,?,?,?)";
		Object[] params = new Object[]{job.getJobId(),job.getUserName(),job.getJobItemPath(),job.getStartDate(),job.getEndDate(),job.getModifyDate(),job.getCostPrice().floatValue(),job.getGetPrice().floatValue(),job.getState().intValue()};
	    new DBUtils().save(sql, params);
	}
	
	/**
	 * 获取job.
	 * 
	 * @param jobId
	 * @return
	 */
	public Job getJob(Long jobId){
		String sql = "select * from t_job where jobId =?";
		Object[] params = new Object[]{jobId};
		List<Job> jobs = new DBUtils().query(sql, Job.class, params);
		if(jobs.size()>0){
			Job job = jobs.get(0);
			return job;
		}else{
			return null;
		}
		
	}
	
	/**
	 * 更新job.
	 * 
	 * @param job
	 */
	public void update(Job job){
		String sql = "update t_job set modifyDate = ?,endDate=?,state=? where jobId=?";
		Object[] params = new Object[]{job.getModifyDate(),job.getEndDate(),job.getState().intValue(),job.getJobId().longValue()};
		new DBUtils().update(sql, params);
	}
	
	/**
	 * 获取任务列表.
	 * 
	 * @param userName
	 * @return
	 */
	public List<Job> getJobList(String userName){
		String sql = "select * from t_job where userName=?";
		Object[] params = new Object[]{userName};
		List<Job> jobs = new DBUtils().query(sql, Job.class, params);
		return jobs;
	}

}
